package vn.edu.nuce.bean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;

import vn.edu.nuce.model.Email;
import vn.edu.nuce.model.LichDangKy;
import vn.edu.nuce.model.LichDangKyStatus;
import vn.edu.nuce.model.PhongMay;
import vn.edu.nuce.model.TietHoc;
import vn.edu.nuce.service.EmailService;
import vn.edu.nuce.service.LichDangKyService;
import vn.edu.nuce.service.PhongMayService;
import vn.edu.nuce.service.TietHocService;
import vn.edu.nuce.service.UserService;
import vn.edu.nuce.utility.Utility;

@ManagedBean(name = "lichDangKyBean")
@ViewScoped
public class LichDangKyBean {
	@ManagedProperty(value = "#{LichDangKyService}")
	LichDangKyService lichDangKyService;
	@ManagedProperty(value = "#{TietHocService}")
	TietHocService tietHocService;
	@ManagedProperty(value = "#{PhongMayService}")
	PhongMayService phongMayService;
	@ManagedProperty(value = "#{UserService}")
	UserService userService;
	@ManagedProperty(value = "#{EmailService}")
	EmailService emailService;

	private TietHoc tietHocSelected;
	private PhongMay phongMaySelected;
	private LichDangKyStatus lichDkSelected;
	
	private String userId = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.AUTH_ID);

	List<TietHoc> tietHocList;
	List<PhongMay> phongMayList;
	List<LichDangKy> dsChoList;

	List<LichDangKyStatus> lichDangKyStatusList;

	LichDangKy ettLichDangKy = new LichDangKy();

	static public String convertStatus(String status) {
		String value = "";
		if (status.equals("0")||status.equals("-2")) {
			value = "Đang chờ xử lý";
		} else if (status.equals("1")) {
			value = "Đã phê duyệt";
		} else if (status.equals("-1")){
			value = "Đã hủy";
		}
		return value;
	}
	
	public void updateTietHoc(){
		if (lichDkSelected!=null && lichDkSelected.getIdTietHoc()!=null){
			ettLichDangKy.setIdTietHoc(lichDkSelected.getIdTietHoc());
		}
	}

	public void addLichDangKy() {		
		FacesContext context = FacesContext.getCurrentInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date currentDate;
		try {
			currentDate = sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
			currentDate = new Date();
		}
		List<LichDangKy> dsTrung = getLichDangKyService().hienThiDangKyTrungTheoNgay(currentDate);
		boolean check=false;
		for (LichDangKy item:dsTrung){
			if (item.getStatus()==1){//check dang ky trung da duoc phe duyet hay chua
				check = true;
				break;
			}
		}
		
		//validate
		if (ettLichDangKy.getTieuDe().trim().equals("")){
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Tiêu đề không được để trống",null));
		} else if (ettLichDangKy.getNgayDat().before(currentDate)){
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Đã qua ngày đăng ký",null));
		} else if (check){
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Đã có đăng ký được phê duyệt",null));
		} else {
			ettLichDangKy.setId(Utility.genUUID());
			ettLichDangKy.setStatus(0);
			ettLichDangKy.setIdGiaoVien(userId);
			ettLichDangKy.setTenGiaoVien(getUserService().getById(userId).getTenUser());
			ettLichDangKy.setTenPhongMay(getPhongMayService().getById(ettLichDangKy.getIdPhong()).getTenPhongMay());
			ettLichDangKy.setMoTaTietHoc(getTietHocService().getById(ettLichDangKy.getIdTietHoc()).getMoTa());
			String emailLop = ettLichDangKy.getEmailLop();
			ettLichDangKy.setEmailLop(getUserService().getById(userId).getEmail());
			getLichDangKyService().addLichDangKy(ettLichDangKy);
			
			Email email = new Email();
			email.setIdLichDangKy(ettLichDangKy.getId());
			email.setEmail(emailLop);
			getEmailService().addEmail(email);
			
			Date tmpDate = ettLichDangKy.getNgayDat();
			ettLichDangKy = new LichDangKy();
			ettLichDangKy.setNgayDat(tmpDate);
			
			
			context.addMessage(null, new FacesMessage("Đăng ký thành công"));
			
			RequestContext.getCurrentInstance().execute("dlg.hide()");
		}		
	}
	
	public void huyDangKy(String id) {
		LichDangKy lichDangKy = getLichDangKyService().getLichDangKyById(id);
		FacesContext context = FacesContext.getCurrentInstance();
		if (lichDangKy.getIdGiaoVien().equals(userId)){
			getLichDangKyService().deleteLichDangKy(id);		
			context.addMessage(null, new FacesMessage("Đã hủy đăng ký"));
		} else{
			context.addMessage(null, new FacesMessage("Bạn không thể hủy đăng ký này"));
		}		
	}

	public void handleDateSelect(SelectEvent event) {
		Date date = (Date) event.getObject();
		ettLichDangKy.setNgayDat(date);
	}

	public void onTabChange(TabChangeEvent event) {
		if (event.getTab().getId().equals("tab2")) {
			tietHocSelected = getTietHocList().get(Integer.parseInt(ettLichDangKy.getIdTietHoc())-1);
			phongMaySelected = getPhongMayList().get(Integer.parseInt(ettLichDangKy.getIdPhong())-1);
		} else if (event.getTab().getId().equals("tab3")) {
			phongMaySelected = getPhongMayList().get(Integer.parseInt(ettLichDangKy.getIdPhong())-1);
		}
	}

	public List<LichDangKyStatus> getLichDangKyStatusList() {
		return getLichDangKyService().getLichDangKyStatusByDate(
				getEttLichDangKy().getNgayDat());
	}

	public void setLichDangKyStatusList(
			List<LichDangKyStatus> lichDangKyStatusList) {
		this.lichDangKyStatusList = lichDangKyStatusList;
	}

	public List<PhongMay> getPhongMayList() {
		phongMayList = getPhongMayService().getPhongMayHoatDong();
		return phongMayList;
	}

	public void setPhongMayList(List<PhongMay> phongMayList) {
		this.phongMayList = phongMayList;
	}
	
	public List<TietHoc> getTietHocList() {
		tietHocList = getTietHocService().getTietHocList();
		return tietHocList;
	}

	public void setTietHocList(List<TietHoc> tietHocList) {
		this.tietHocList = tietHocList;
	}

	public List<LichDangKy> getDsChoList() {
		dsChoList = getLichDangKyService().getDsCho(ettLichDangKy.getIdPhong(),
				ettLichDangKy.getIdTietHoc(), ettLichDangKy.getNgayDat());
		return dsChoList;
	}

	public void setDsChoList(List<LichDangKy> dsChoList) {
		this.dsChoList = dsChoList;
	}

	public PhongMayService getPhongMayService() {
		return phongMayService;
	}

	public void setPhongMayService(PhongMayService phongMayService) {
		this.phongMayService = phongMayService;
	}

	public TietHocService getTietHocService() {
		return tietHocService;
	}

	public void setTietHocService(TietHocService tietHocService) {
		this.tietHocService = tietHocService;
	}

	public LichDangKyService getLichDangKyService() {
		return lichDangKyService;
	}

	public void setLichDangKyService(LichDangKyService lichDangKyService) {
		this.lichDangKyService = lichDangKyService;
	}

	public TietHoc getTietHocSelected() {
		return tietHocSelected;
	}

	public void setTietHocSelected(TietHoc tietHocSelected) {
		this.tietHocSelected = tietHocSelected;
		ettLichDangKy.setIdTietHoc(tietHocSelected.getIdTietHoc());
	}
	
	public PhongMay getPhongMaySelected() {
		return phongMaySelected;
	}

	public void setPhongMaySelected(PhongMay phongMaySelected) {
		this.phongMaySelected = phongMaySelected;
	}

	public LichDangKy getEttLichDangKy() {
		return ettLichDangKy;
	}

	public void setEttLichDangKy(LichDangKy ettLichDangKy) {
		this.ettLichDangKy = ettLichDangKy;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public EmailService getEmailService() {
		return emailService;
	}

	public void setEmailService(EmailService emailService) {
		this.emailService = emailService;
	}

	public LichDangKyStatus getLichDkSelected() {
		return lichDkSelected;
	}

	public void setLichDkSelected(LichDangKyStatus lichDkSelected) {
		this.lichDkSelected = lichDkSelected;
	}
	
}
